package org.threadCase2;

import java.util.concurrent.locks.Lock;
import java.util.concurrent.locks.ReentrantLock;

public class ThreadDemo implements Runnable{
    public static int gift=0;
    private Lock lock=new ReentrantLock();
    @Override
    public void run() {
        while (true){
            lock.lock();
            try {
                if (gift==90){
                    break;
                }else {
                    Thread t=Thread.currentThread();
                    Thread.sleep(500);
                    gift++;
                    System.out.println(t.getName()+"送出了"+gift+"份礼物。还剩"+(100-gift)+"份礼物");
                }
            } catch (InterruptedException e) {
                throw new RuntimeException(e);
            } finally {
                lock.unlock();
            }
        }
    }
}
